package com.yixin.hospital.controller;

import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.dto.req.purchaseorder.GoDownDrugReqDTO;
import com.yixin.hospital.entity.dto.req.purchaseorder.PurchaseOrderAddReqDTO;
import com.yixin.hospital.service.PurchaseOrderService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/purchase-order")
@Api(value = "采购单管理", tags = "采购单管理")
public class PurchaseOrderController {

    @Resource
    private PurchaseOrderService purchaseOrderService;

    @GetMapping("/list")
    @ApiOperation(value = "采购单列表")
    public ApiResult<?> list(@RequestParam int currentPage, @RequestParam int pageSize,
                             @RequestParam(value = "status", required = false) Integer status,
                             @RequestParam(value = "deptId", required = false) Integer deptId,
                             @RequestParam(value = "type", required = false) String type,
                             HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return purchaseOrderService.allList(currentPage, pageSize, status, deptId, type, userId);
    }

    @GetMapping("/detail/{id}")
    @ApiOperation(value = "采购单详情")
    public ApiResult<?> detail(@PathVariable int id) {
        return purchaseOrderService.detail(id);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除采购单")
    @Transactional
    public ApiResult<?> delete(@PathVariable int id) {
        return purchaseOrderService.delete(id);
    }

    @PostMapping("/addorupdate")
    @ApiOperation(value = "添加/编辑采购单")
    @Transactional
    public ApiResult<?> add(@Valid @RequestBody PurchaseOrderAddReqDTO purchaseOrderAddReqDTO, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return purchaseOrderService.add(purchaseOrderAddReqDTO, userId, userName);
    }

    @PostMapping("/godown-drug")
    @ApiOperation(value = "入库")
    @Transactional
    public ApiResult<?> goDownDrug2(@Valid @RequestBody GoDownDrugReqDTO goDownDrugReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return purchaseOrderService.godownDrug(goDownDrugReqDTO, userName);
    }

}
